<?php

namespace Home\Controller;

use Think\Controller;
// require_once './Application/Admin/Controller/SortController.class.php';
use Admin\Model\SortModel;

class IndexController extends Controller {

    public function index() {
        $keyword = I('get.keyword');
        $where['keyword'] = ['like', '%' . $keyword . '%'];
        $where['is_show'] = 1;
        $article = D('article');
        $count = $article->count();
        $Page = new \Think\Page($count, 5);
        $Page->setConfig('header', '<span class="rows">共<b>%TOTAL_ROW%</b>条记录 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</span>');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $Page->show();
        $result = $article->alias("a")
                ->field(['a.*', 's.class'])
                ->where($where)
                ->join('sort as s on a.s_id=s.id')
                ->order('a.is_top desc')
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();

        // var_dump($data);die;

        $sort = D('sort');
        $res = $sort->where('status=0')->select();

        $new = new SortModel;

        $res = $new->findSubTree($res);
        $res = $new->tree($res);
//         var_dump($res);die;

        $show = $Page->show();
        $this->assign('res', $res);
        $this->assign('show', $show);
        $this->assign('result', $result);
        $this->display();
    }

    //文章详情
    public function lists() {
        $id = I('get.id');

        $comment = D('comment');
        $article = D('article');
        $sort = D('sort');
        $res = $sort->where('status=0')->select();
        $result = $article->alias("a")
                ->field(['a.*', 's.class'])
                ->where($where)
                ->join('sort as s on a.s_id=s.id')
                ->order('a.is_top desc')
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
        $new = new SortModel;

        $res = $new->findSubTree($res);
        $res = $new->tree($res);


        $cmt = $comment->where('a_id=' . $id)->select();
//         var_dump($cmt);die;
        
        // $data = S('blog' . $id);//memcache取值
//        var_dump($data);die;
        // if (empty($data)) {
            $data = $article->where('id=' . $id)->select();
           
            // S('blog' . $id, $data);
        // }

        $article->where('id=' . $id)->setInc('click', 1); 
//        //点击数
//        $click['click'] = $data[0]['click'] + 1;
//        $article->where('id=' . $id)->data($click)->save();
//        $s_id = $data[0]['s_id'];
//        //当前文章所在目录
//        $class = $sort->where('id=' . $s_id)->field('class')->select();
//        $data[0]['class'] = $class[0]['class'];

        $this->assign('res', $res);



        $this->assign('data', $data);
        //评论
        $this->assign('cmt', $cmt);
        //所有文章
        $this->assign('result', $result);

        $this->display();
    }

    public function seek() {
        
    }

}
